package com.yh.homestay.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.dao.CapitalGoodsMapper;
import com.yh.homestay.pojo.CapitalGoods;
import com.yh.homestay.service.CapitalGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @ClassName CapitalGoodsServiceImpl
 * @Description 物资业务逻辑
 * @Date 2020/1/2 10:32
 */
@Service
public class CapitalGoodsServiceImpl implements CapitalGoodsService {

    private Log logger = LogFactory.getLog(CapitalRoomServiceImpl.class);

    @Autowired
    private CapitalGoodsMapper capitalGoodsMapper;

    /**
     * @param page
     * @param limit
     * @param start
     * @param end
     * @param goodsName
     * @Date: 2020/1/11 10:34
     * @Description: 查询出所有物资信息
     * @return: com.yh.homestay.common.HomeStayResult
     */
    @Override
    public HomeStayResult findAllCapitalGoods(Integer page, Integer limit, String start, String end, String goodsName) {
        try {
            PageHelper.startPage(page, limit);
            List<CapitalGoods> capitalGoods;
            if (StringUtils.isEmpty(goodsName)) {
                capitalGoods = capitalGoodsMapper.selectAll();
            } else {
                Example example = new Example(CapitalGoods.class);
                example.createCriteria().andLike("goodsName", "%" + goodsName + "%");
                capitalGoods = capitalGoodsMapper.selectByExample(example);
            }
            PageInfo info = new PageInfo(capitalGoods);
            return new HomeStayResult(0, "ok", (int) info.getTotal(), info.getList());
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param good
     * @Date: 2020/1/11 10:34
     * @Description: 添加物资逻辑
     * @return: com.yh.homestay.common.CapitalGoodsResult
     */
    @Override
    public HomeStayResult addCapitalGood(CapitalGoods good) {
        try {
            capitalGoodsMapper.insertSelective(good);
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param goodsNumber 物资编号
     * @Date: 2020/1/11 10:35
     * @Description: 删除物资逻辑
     * @return: com.yh.homestay.common.CapitalGoodsResult
     */
    @Override
    public HomeStayResult delCapitalGood(String goodsNumber) {
        try {
            Example example = new Example(CapitalGoods.class);
            example.createCriteria().andEqualTo("goodsNumber", goodsNumber);
            capitalGoodsMapper.deleteByExample(example);
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param nums 多个房间编号
     * @Date: 2020/1/11 10:36
     * @Description: 批量删除物资逻辑
     * @return: com.yh.homestay.common.CapitalGoodsResult
     */
    @Override
    public HomeStayResult delBatchCapitalGood(String nums) {
        try {
            String[] num = nums.split(",");
            for (int i = 0; i < num.length; i++) {
                Example example = new Example(CapitalGoods.class);
                example.createCriteria().andEqualTo("goodsNumber", num[i]);
                capitalGoodsMapper.deleteByExample(example);
            }
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }

    }

    /**
     * @param num
     * @Date: 2020/1/11 10:48
     * @Description: 查询出单个物资信息
     * @return: com.yh.homestay.common.CapitalGoodsResult
     */
    @Override
    public HomeStayResult seleteOneCapitalGood(String num) {
        try {
            Example example = new Example(CapitalGoods.class);
            example.createCriteria().andEqualTo("goodsNumber", num);
            CapitalGoods capitalGoods = capitalGoodsMapper.selectByExample(example).get(0);
            return HomeStayResult.ok(200, capitalGoods);
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param good
     * @Date: 2020/1/11 10:50
     * @Description: 更新物资信息
     * @return: com.yh.homestay.common.CapitalGoodsResult
     */
    @Override
    public HomeStayResult updataCapitalGood(CapitalGoods good) {
        try {
            capitalGoodsMapper.updateByPrimaryKeySelective(good);
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

}
